﻿' ------------------------------------------------------------------------------
' This UserControl manages database connection information, shuttling it between
' My.Settings, the Control, the user, and back. When the user clicks the "Connect"
' button, the control raises the OkButtonClicked event, allowing a client form 
' to perform actions when the connection changes.
'
' When the user chooses to connect to or disconnect from the database, the 
' SpatialAlbum.Core.Connection object in g_connection is given the new connection
' information and asked to connect, or asked to disconnect.
' ------------------------------------------------------------------------------
' This file is part of the SQL Server 2008 Technology Demonstration. This
' application was designed and coded by Eric Weitzman, LandTime Inc, for
' Microsoft Corporation. Contact Ed Katibah at Microsoft about this demo.
' Copyright © 2008 Microsoft Corporation. All rights reserved.
' ------------------------------------------------------------------------------

Public Class ConnectControl
    Inherits System.Windows.Forms.UserControl

    Event OkButtonClicked()

    Private Sub ConnectionControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If DesignMode Then Return
        If g_connection Is Nothing Then Return

        txtServerName.Text = g_connection.Server
        txtDatabase.Text = g_connection.Database
        Select Case g_connection.Auth
            Case EnumConnectionAuth.eSqlServer : rdoAuthSqlServer.Checked = True
            Case Else : rdoAuthWindows.Checked = True
        End Select
        txtLogin.Text = g_connection.Login
        txtPassword.Text = g_connection.Password

        SyncFields(g_connection.Auth = EnumConnectionAuth.eSqlServer)

        AddHandler g_connection.ConnectionChanged, AddressOf OnConnectionChanged

    End Sub

    Private Sub btnConnectDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectDisconnect.Click
        If g_connection.Connected Then
            g_connection.Disconnect()
        Else
            g_connection.Server = txtServerName.Text
            g_connection.Database = txtDatabase.Text
            If rdoAuthSqlServer.Checked Then g_connection.Auth = EnumConnectionAuth.eSqlServer
            If rdoAuthWindows.Checked Then g_connection.Auth = EnumConnectionAuth.eWindows
            g_connection.Login = txtLogin.Text
            g_connection.Password = txtPassword.Text

            g_connection.Connect()
        End If
    End Sub

    Private Sub OnConnectionChanged()
        'Trace.WriteLine("ConnectionControl: ConnectionChanged")
        SyncFields(g_connection.Auth = EnumConnectionAuth.eSqlServer)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SaveConnectionInfo()
        My.Settings.Save()
    End Sub

    Private Sub btnForgetClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForget.Click
        ForgetConnectionInfo()
        My.Settings.Save()
    End Sub

    Private Sub rdoAuthWindows_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoAuthWindows.CheckedChanged
        SyncFields(rdoAuthSqlServer.Checked)
    End Sub

    Private Sub rdoAuthSqlServer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoAuthSqlServer.CheckedChanged
        SyncFields(rdoAuthSqlServer.Checked)
    End Sub

    Private Sub SyncFields(ByVal sqlserverauth As Boolean)
        txtLogin.Enabled = sqlserverauth
        txtPassword.Enabled = sqlserverauth
        btnSave.Enabled = g_connection.Connected
        btnConnectDisconnect.Text = DirectCast(IIf(g_connection.Connected, "Disconnect", "Connect"), String)
        lblConnectionStatus.Text = DirectCast(IIf(g_connection.Connected, "You are connected to the database.", "Not connected."), String)
        btnConnectDisconnect.Enabled = txtServerName.Text <> "" And txtDatabase.Text <> ""
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        RaiseEvent OkButtonClicked()
    End Sub

    Private Sub txtServerName_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtServerName.KeyUp
        SyncFields(rdoAuthSqlServer.Checked)
    End Sub

    Private Sub txtDatabase_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDatabase.KeyUp
        SyncFields(rdoAuthSqlServer.Checked)
    End Sub

    Public Sub SaveConnectionInfo()
        My.Settings.CONNECT_Server = g_connection.Server
        My.Settings.CONNECT_Database = g_connection.Database
        My.Settings.CONNECT_Auth = SpatialAlbum.Core.AuthToString(g_connection.Auth)
        My.Settings.CONNECT_Login = g_connection.Login
        My.Settings.CONNECT_Password = SpatialAlbum.Core.EncryptPassword(g_connection.Password)
        My.Settings.Save()
        Trace.WriteLine("saved connection info")
    End Sub

    Public Sub ForgetConnectionInfo()
        My.Settings.CONNECT_Server = ""
        My.Settings.CONNECT_Database = ""
        My.Settings.CONNECT_Auth = EnumConnectionAuth.eUnknown.ToString
        My.Settings.CONNECT_Login = ""
        My.Settings.CONNECT_Password = ""
        My.Settings.Save()
        Trace.WriteLine("forget (saved empty) connection info")
    End Sub
End Class
